package mybatis;

import org.smallframework.spring.path.ClassPathScanningUtils;
import sf.database.jdbc.sql.SQLContext;
import sf.database.template.mybatis.MybatisHelp;
import sf.database.template.sql.SqlHelp;
import sf.tools.IOUtils;

import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class T2 {

    public static void main(String[] args) {
        Set<URL> sets = ClassPathScanningUtils.getPathScanning().getResourcesURL(null, "classpath*:sql/mybatis.md");
        for (URL r : sets) {
            try {
                List<String> lines = new ArrayList<>();
                IOUtils.asStringLambda(r.openStream(), StandardCharsets.UTF_8, true, line -> {
                    lines.add(line);
                    return true;
                });
                //必须有内容
                Map<String, String> map = SqlHelp.fromMarkdown(lines);
                MybatisHelp.getInstance().putAllTemplate(map);

                List<Object> para = new ArrayList<>();
                Map<String, Object> params = new HashMap<>();
                params.put("id", "1");
                params.put("username", "2");
                params.put("nicename", "3");
                List<String> displayNames = new ArrayList<>();
                displayNames.add("SS");
                displayNames.add("DDD");
                params.put("displayNames", displayNames);
                long now = System.currentTimeMillis();
                String sql = MybatisHelp.getInstance().render("queryUserByNamePage", null, params, para);
                System.out.println((System.currentTimeMillis() - now) + "ms");
                System.out.println(sql);
                now = System.currentTimeMillis();
                SQLContext context = MybatisHelp.getInstance().getSqlParaPage("queryUserByNamePage", null, params);
                System.out.println((System.currentTimeMillis() - now) + "ms");
                now = System.currentTimeMillis();
                context = MybatisHelp.getInstance().getSqlParaPage("queryUserByNamePage2", null, params);
                System.out.println((System.currentTimeMillis() - now) + "ms");
                System.out.println(context.getCountSql());
                System.out.println(context.getListSql());
                System.out.println(para);
                System.out.println(map);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
